package com.xiaotu.storage;

import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import com.xiaotu.storage.model.CacheDataModel;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.stream.Stream;

/**
 *  猫眼影投票房数据转储实现类
 */
@Component("MaoYanMovieCompanyStorageProcessor")
public class MaoYanMovieCompanyStorageProcessor extends AHSetStorageProcessor
{
	
	private static final String[] FIELD_ARR = new String[]{"id","tvname","dataId", "siteName", "areaName",
            "boxoffice", "boxofficeRatio", "sliceratio", "avgNumber", "totalBoxoffice",
            "seatProportion", "goldenRate", "peopleNum", "screen", "timestamp",
            "avgSeatView","goldenScreen"};
	
	private static final String[] CONVERT_ARR = new String[]{
    		AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.NUMBER_CONVERT,
            AConverter.RATE_CONVERT, AConverter.RATE_CONVERT, AConverter.NUMBER_CONVERT,
            AConverter.NUMBER_CONVERT, AConverter.RATE_CONVERT, AConverter.RATE_CONVERT,
            AConverter.NUMBER_CONVERT, AConverter.NUMBER_CONVERT, AConverter.TIMESTAMP_CONVERT,
            AConverter.RATE_CONVERT,AConverter.NUMBER_CONVERT
            };
	
	private static final boolean[] THROW_ARR = new boolean[]{true,true,true, true, true, true, true, true,
            true, true, true, true, true, true, true , true, true};
	
    @Override
    protected String getRedisKey(Date date)
    {
        return DataRedisKeyUtils.getKey(Constants.DataRedisKey.MAOYAN_MOVIE_COMPANY_YYYYMMDD, date);
    }

    @Override
    public String getDataType()
    {
        return Constants.PageDataType.MOVIE_COMPANY;
    }

	@Override
	public String process(JSONObject target, CacheDataModel cacheData, Date date) {
		//this.validateAllNotEmpty(target, FIELD_ARR);
        Stream.of(FIELD_ARR).forEach(field ->
        {
            if (target.getString(field).contains("--")||StringUtils.isBlank(target.getString(field)))
                target.put(field, null);
        });
        return this.list2CSVLine(ConverterFactory.convert(target, FIELD_ARR, CONVERT_ARR,
                THROW_ARR), SepratorUtil.SEP_PIPE);
	}

	@Override
	public String getTaskType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

}
